package main

import (
	report2 "agent-service/report"
	"context"
	"flag"
	"fmt"
	"net"
	"time"

	"google.golang.org/grpc"
)

type Server struct {
	report2.UnimplementedReportServiceServer
}

func (s *Server) Report(ctx context.Context, request *report2.ReportReq) (*report2.ReportRsp, error) {
	return &report2.ReportRsp{
		Code: 1,
		Msg:  "成功获取",
	}, nil
}

// tickerHandler 阻塞定时循环
func tickerHandler() {
	// NewTicker 每隔5s,想tk.C发送一个当前时间
	tk := time.NewTicker(60 * time.Second)
	defer tk.Stop()
	// for循环
	for {
		// 每次从tk.C取出一次执行echoDone
		select {
		case <-tk.C:
			fmt.Println("ticker expired")
		}
	}
}

func main() {
	// 把用户传递的命令行参数解析为对应变量的值
	flag.Parse()
	g := grpc.NewServer()
	report2.RegisterReportServiceServer(g, &Server{})
	lis, err := net.Listen("tcp", "0.0.0.0:8088")
	if err != nil {
		panic("faild to start grpc" + err.Error())
	}
	// NewTicker 每隔5s,想tk.C发送一个当前时间
	/*	tk := time.NewTicker(5 * time.Second)
		defer tk.Stop()
		// for循环
		for {
			// 每次从tk.C取出一次执行echoDone
			select {
			case <-tk.C:
				err = g.Serve(lis)
				if err != nil{
					panic("faild to start grpc" + err.Error())
				}		}
		}*/
	err = g.Serve(lis)
	if err != nil {
		panic("faild to start grpc" + err.Error())
	}
	/*
		flag.Parse()
		lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
		if err != nil {
			log.Fatalf("failed to listen: %v", err)
		}
		var opts []grpc.ServerOption
		if *tls {
			if *certFile == "" {
				*certFile = data.Path("x509/server_cert.pem")
			}
			if *keyFile == "" {
				*keyFile = data.Path("x509/server_key.pem")
			}
			creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
			if err != nil {
				log.Fatalf("Failed to generate credentials %v", err)
			}
			opts = []grpc.ServerOption{grpc.Creds(creds)}
		}
		grpcServer := grpc.NewServer(opts...)
		pb.RegisterReportServiceServer(grpcServer, newServer())
		grpcServer.Serve(lis)

	*/
}
